package com.example.howard.xiao7music.service;

import android.app.DownloadManager;
import android.app.Service;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;

import com.example.howard.xiao7music.bean.SingerSongBean;
import com.example.howard.xiao7music.bean.SongDownloadBean;
import com.example.howard.xiao7music.bean.SongRankingBean;
import com.example.howard.xiao7music.model.GetRemoteSource;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class DownloadService extends Service {

    public static final String DOWNLOADPATH= Environment.getExternalStorageDirectory()+"/小7音乐盒下载";
    private GetRemoteSource getRemoteSource=new GetRemoteSource(new GetRemoteSource.callback() {

        @Override
        public void setRankingSongBean(SongRankingBean bean) {

        }

        @Override
        public void setSingerSongBean(SingerSongBean bean) {

        }

        @Override
        public void setPlaySongURL(String url) {

        }

        @Override
        public void setDownloadSongBean(SongDownloadBean bean) {

            songDownloadBean=bean;
            setDownload(songDownloadBean);
        }
    });

    private SongDownloadBean songDownloadBean;



    public DownloadService() {
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        String songID = intent.getStringExtra("songID");
        long songid= Long.parseLong(songID);
        getJSON(songid);
    }

    private void getJSON(final long songid){

//        SongDownloadBean bean;
        getRemoteSource.getdownloadsong(songid);


    }
    private void setDownload(SongDownloadBean songDownloadBean){
        String songname;
        String path;

        path=songDownloadBean.getBitrate().get(0).getShow_link();
        if (path.equals("")||path==null){
            path=songDownloadBean.getBitrate().get(0).getFile_link();
        }
        songname=songDownloadBean.getSonginfo().getTitle()+"."+songDownloadBean.getBitrate().get(0).getFile_extension();
//        Log.e("CALL",songID);
//        Log.e("CALL",path);
        if (path!=null){
            downloadsong(path,songname);
        }
    }



    private void downloadsong(String path,String songname){
        Log.e("downstart",path);
        DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        DownloadManager.Request downloadsong=new  DownloadManager.Request(Uri.parse(path));
        //移动和无线wifi都可以下载
//        downloadsong.setAllowedNetworkTypes((DownloadManager.Request.NETWORK_MOBILE|DownloadManager.Request.NETWORK_WIFI));
        //只有WIFI可以下载
        downloadsong.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
        //发出通知，既后台下载
        downloadsong.setDescription("小7音乐盒歌曲下载");
        downloadsong.setTitle("小7音乐盒歌曲");
        //设置下载后文件存放的位置
        downloadsong.setDestinationInExternalPublicDir(DOWNLOADPATH,songname);
        //将下载请求放入队列
        downloadManager.enqueue(downloadsong);
    }


    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}
